package com.itjin.health.controller;

import com.itjin.health.common.BaseResponse;
import com.itjin.health.common.ResultUtils;
import com.itjin.health.domain.dto.timeslot.AvailableSlotPublishRequest;
import com.itjin.health.domain.dto.timeslot.LocationAddRequest;
import com.itjin.health.domain.dto.timeslot.TimeSlotAddRequest;
import com.itjin.health.domain.entity.AvailableSlot;
import com.itjin.health.domain.entity.Location;
import com.itjin.health.domain.entity.TimeSlot;
import com.itjin.health.exception.BusinessException;
import com.itjin.health.exception.ErrorCode;
import com.itjin.health.service.TimeAndLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/time-location")
@Api(tags = "时间地点管理接口")
@Slf4j
public class TimeAndLocationController {

  @Resource
  private TimeAndLocationService timeAndLocationService;

  /**
   * 管理员添加时间段
   */
  @PostMapping("/time-slot/add")
  @ApiOperation(value = "添加时间段")
  public BaseResponse<TimeSlot> addTimeSlot(@RequestBody TimeSlotAddRequest timeSlotAddRequest,
      HttpServletRequest request) {
    // 验证是否管理员
    validateAdmin(request);
    TimeSlot timeSlot = timeAndLocationService.addTimeSlot(timeSlotAddRequest);
    return ResultUtils.success(timeSlot);
  }

  /**
   * 查询所有时间段
   */
  @GetMapping("/time-slot/list")
  @ApiOperation(value = "查询所有时间段")
  public BaseResponse<List<TimeSlot>> listTimeSlots() {
    List<TimeSlot> timeSlots = timeAndLocationService.listTimeSlots();
    return ResultUtils.success(timeSlots);
  }

  /**
   * 管理员添加地点
   */
  @PostMapping("/location/add")
  @ApiOperation(value = "添加地点")
  public BaseResponse<Location> addLocation(@RequestBody LocationAddRequest locationAddRequest,
      HttpServletRequest request) {
    // 验证是否管理员
    validateAdmin(request);
    Location location = timeAndLocationService.addLocation(locationAddRequest);
    return ResultUtils.success(location);
  }

  /**
   * 查询所有地点
   */
  @GetMapping("/location/list")
  @ApiOperation(value = "查询所有地点")
  public BaseResponse<List<Location>> listLocations() {
    List<Location> locations = timeAndLocationService.listLocations();
    return ResultUtils.success(locations);
  }

  /**
   * 管理员发布可用时间段和地点
   */
  @PostMapping("/available-slot/publish")
  @ApiOperation(value = "发布可用时间段和地点", notes = "管理员为特定周发布可用时间段和地点")
  public BaseResponse<Boolean> publishAvailableSlots(@RequestBody AvailableSlotPublishRequest publishRequest,
      HttpServletRequest request) {
    // 验证是否管理员
    Integer adminId = validateAdmin(request);

    // 发布可用时间段和地点
    timeAndLocationService.publishAvailableSlots(publishRequest, adminId);
    return ResultUtils.success(true);
  }

  /**
   * 查询当前周和未来几周的可用时间段和地点
   */
  @GetMapping("/available-slot/list")
  @ApiOperation(value = "查询可用时间段和地点", notes = "查询当前周和未来几周的可用时间段和地点")
  public BaseResponse<Map<String, List<AvailableSlot>>> listAvailableSlots(
      @ApiParam(value = "评估类型: 线上/线下") @RequestParam String assessmentType,
      @ApiParam(value = "未来几周, 默认为4周") @RequestParam(required = false, defaultValue = "4") Integer futureWeeks) {

    Map<String, List<AvailableSlot>> result = timeAndLocationService.listAvailableSlots(assessmentType, futureWeeks);
    return ResultUtils.success(result);
  }

  /**
   * 验证用户是否为管理员或教师
   */
  private Integer validateAdmin(HttpServletRequest request) {
    // 获取当前登录用户ID
    Object userIdObj = request.getSession().getAttribute("userId");
    if (userIdObj == null) {
      throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
    }
    Integer userId = (Integer) userIdObj;

    // 获取角色
    String userRole = (String) request.getSession().getAttribute("userRole");
    if (!"管理员".equals(userRole) && !"教师".equals(userRole) && !"超级管理员".equals(userRole)) {
      throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无管理员权限");
    }

    return userId;
  }
}
